<?php
/**
 * Created by PhpStorm.
 * User: Sunmer
 * Date: 2019/6/1 0001
 * Time: 23:44
 * Email:Sunmer@foxmail.com
 */
namespace app\api\controller;

use think\Db;

class Retrieve extends BaseController{
    /**
     * 更新密码
     * @return \think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function retrieve()
    {
        //接受参数
        $res = request()->param();
        //检测数据
        if (empty($res['mobile'])||empty($res['code'])||empty($res['password']))
        {
            return $this->ajax_return('null','参数不完整','201');
        }
        //校验数据
        //手机号校验
        if (!Db::table('tp_code')->where('member_mobile',$res['mobile'])->find())
        {
            return $this->ajax_return('null','手机号不匹配','201');
        }
        //验证码校验
        if (!Db::table('tp_code')->where('code',$res['code'])->find())
        {
            return $this->ajax_return('null','验证码不正确','201');
        }
        //处理数据
        $data = [
          'member_password'=>$this->password($res['password']),
          'update_at'=>date('Y-m-d H:i:s',time()),
        ];
        $result = DB::table('tp_user')->where('member_mobile',$res['mobile'])->update($data);
        if ($result)
        {
            return $this->ajax_return($data,'更改密码成功','200');
        }else
        {
            return $this->ajax_return('null','更改密码失败','201');
        }
    }
}